package edu.uta.iconverter.ui;

import java.awt.*; 
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import edu.uta.iconverter.converter.*;

public class GUIConverter extends JPanel implements ActionListener {
    
    static private final String newline = "\n";
    JButton openButton, convertButton;
    JTextArea javaText;
    JTextArea cSharpText;
    JFileChooser fc;
    String javaFilePath = "";
 
    public GUIConverter() {
        super(new BorderLayout());
 
        //Create the log first, because the action listeners
        //need to refer to it.
        javaText = new JTextArea(50,50);
        javaText.setMargin(new Insets(10,10,10,10));
        javaText.setEditable(false);
        JScrollPane javaScrollPane = new JScrollPane(javaText);
        
        cSharpText = new JTextArea(50,50);
        cSharpText.setMargin(new Insets(5,5,5,5));
        cSharpText.setEditable(false);
        JScrollPane cSharpScrollPane = new JScrollPane(cSharpText);
        
        //Create a file chooser
        fc = new JFileChooser( System.setProperty("user.dir", ".") );
        //fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        //fc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);

        openButton = new JButton("Choose Java file");
        openButton.addActionListener(this);
 
        //Create the save button.  We use the image from the JLF
        //Graphics Repository (but we extracted it from the jar).
        convertButton = new JButton("Convert");
        convertButton.addActionListener(this);
 
        //For layout purposes, put the buttons in a separate panel
        JPanel buttonPanel = new JPanel(); //use FlowLayout
        buttonPanel.add(openButton);
        buttonPanel.add(convertButton);
 
        //Add the buttons and the log to this panel.
        add(buttonPanel, BorderLayout.PAGE_START);
        add(javaScrollPane, BorderLayout.WEST);
        add(cSharpScrollPane, BorderLayout.EAST);
    }
 
    public void actionPerformed(ActionEvent e) {
    	try {
	    	BufferedReader reader;
	    	
	        //Handle open button action.
	        if (e.getSource() == openButton) {
	            int returnVal = fc.showOpenDialog(GUIConverter.this);
	 
	            if (returnVal == JFileChooser.APPROVE_OPTION) {
	                File file = fc.getSelectedFile();
	                javaFilePath = file.getAbsolutePath();
					reader = new BufferedReader(new FileReader(file));
	                String line = "";
	                String textStr = "";
	                while((line = reader.readLine()) != null)
	                {
	              	  textStr += line;
	              	  textStr += "\r\n";
	                }
	            reader.close();
	      		
	                //This is where a real application would open the file.
	            	javaText.setText("");
	                javaText.append("    JAVA FILE   "  + newline);
	                javaText.append(textStr);
	            } else {
	            	javaText.append("Open command cancelled by user." + newline);
	            }
	            javaText.setCaretPosition(javaText.getDocument().getLength());
	 
	        //Handle save button action.
	        } else if (e.getSource() == convertButton) {
	        	System.out.println("--->> "+javaFilePath);
	        	StringBuffer cSharpBuff = Converter.convert(javaFilePath);
	        	String cSharpStr = cSharpBuff.toString();
	        	cSharpText.setText("");
	        	cSharpText.append("    C# FILE   "  + newline);
	        	cSharpText.append(cSharpStr);
	        }
    	} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
    }
 
    /** Returns an ImageIcon, or null if the path was invalid. */
    protected static ImageIcon createImageIcon(String path) {
        java.net.URL imgURL = GUIConverter.class.getResource(path);
        if (imgURL != null) {
            return new ImageIcon(imgURL);
        } else {
            System.err.println("Couldn't find file: " + path);
            return null;
        }
    }
 
    
    public static void createAndShowGUI() {
        //Create and set up the window.
        JFrame frame = new JFrame("iConverter");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        //Add content to the window.
        frame.add(new GUIConverter());
 
        //Display the window.
        frame.pack();
        frame.setVisible(true);
    }
}
